use axum_app_common::model::PageResult;
use axum_app_macro::QueryPageBuilder;
use sea_orm::{sea_query::Expr, *};
use tracing::error;

use crate::entity::rbac_role_resource_rel::{self, *};

#[derive(Debug, QueryPageBuilder)]
pub struct RbacRoleResourceRelQuery;

/**
 * 查询条件
 */
impl RbacRoleResourceRelQueryCondition {
    pub fn res_type_eq(mut self, user_id: Option<&str>) -> Self {
        self.filter = self.filter.add_option(
            user_id
                .filter(|s| s.trim().len() > 0)
                .map(|s| Expr::col(rbac_role_resource_rel::Column::ResType).eq(s)),
        );
        self
    }

    pub fn role_code_eq(mut self, user_id: Option<&str>) -> Self {
        self.filter = self.filter.add_option(
            user_id
                .filter(|s| s.trim().len() > 0)
                .map(|s| Expr::col(rbac_role_resource_rel::Column::RoleCode).eq(s)),
        );
        self
    }
}

pub async fn insert<C>(
    db: &C,
    role_code: &str,
    res_code: &str,
    res_type: &str,
) -> anyhow::Result<u64>
where
    C: ConnectionTrait,
{
    let model = ActiveModel {
        role_code: Set(role_code.to_string()),
        res_code: Set(res_code.to_string()),
        res_type: Set(res_type.to_string()),
        ..Default::default()
    };

    let res = <Entity as EntityTrait>::insert(model).exec(db).await?;
    Ok(res.last_insert_id as u64)
}

/// 查询条件
impl RbacRoleResourceRelQueryCondition {}
